package com.heima.article.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticalStatisticsMapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.client.article.IArticleClient;
import com.heima.model.common.comment.dtos.CommentManagePageDto;
import com.heima.model.common.dtos.ArticleDto;
import com.heima.model.common.dtos.CommentUpdateStatusDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.article.AdArticleStatistics;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.pojos.article.ApArticleConfig;
import com.heima.model.common.pojos.article.ApAuthor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author 陈辉
 * @data 2024 16:12
 */
@RestController
@Slf4j
public class ArticleClient implements IArticleClient {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApAuthorMapper authorMapper;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticalStatisticsMapper apArticalStatisticsMapper;

    /**
     * 保存或修改article文章信息
     *
     * @param articleDto
     * @return
     */
    @PostMapping("/api/v1/article/sync")
    public ResponseResult syncArticle(@RequestBody ArticleDto articleDto) {
        log.info("article文章同步开始了：{}....",articleDto);
       /* try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return apArticleService.saveOrUpdateArticle(articleDto);
    }

    /**
     * 获取author对象
     * @param userId
     * @return
     */
    @GetMapping("/api/v1/author/{userId}")
    public ResponseResult getOne(Integer userId) {
        ApAuthor apAuthor = authorMapper.selectOne(Wrappers.lambdaQuery(ApAuthor.class).eq(ApAuthor::getUserId, userId));
        return ResponseResult.okResult(apAuthor);
    }

    /**
     * 更新author的wmuserId
     * @param author
     * @return
     */
    @Override
    public ResponseResult update(ApAuthor author) {
        authorMapper.updateById(author);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据作者ID查询
     *
     * @param entryId
     * @return
     */
    @Override
    public ResponseResult getArticleInfo(Long entryId) {
        log.info("根据作者ID查询文章信息开始了：{}....",entryId);
        return apArticleService.getArticleInfo(entryId);
    }

    /**
     * 分页查询当前作者的文章数据
     * @param beginDate
     * @param endDate
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResponseResult newsPage(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd")Date beginDate,
                                   @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                   @PathVariable Integer page,
                                   @PathVariable Integer size,
                                   @PathVariable Integer id) {
        return apArticleService.newPage(beginDate,endDate,page,size,id);
    }

    /**
     * 根据文章id查询文章配置
     * @param articleId
     * @return
     */
    @GetMapping("/api/v1/article/{articleId}")
    public ApArticleConfig getArticleConfig(@PathVariable("articleId") Long articleId){
        log.info("根据文章id查询文章配置: {}",articleId);
        ApArticleConfig config = apArticleConfigMapper.selectOne
                (Wrappers.<ApArticleConfig>lambdaQuery()
                        .eq(ApArticleConfig::getArticleId, articleId));
        return config;

    }

    /**
     * 更改文章评论状态
     *
     * @param statusDto
     * @return
     */
    @Override
    public ResponseResult updateArticleCommentStatus(CommentUpdateStatusDto statusDto) {
        log.info("根据文章ID更改评论状态开启------");
        return apArticleService.updateArticleCommentStatus(statusDto);
    }

    /**
     * 查询符合条件文章
     *
     * @param commentDto
     */
    @Override
    public PageResponseResult findList(CommentManagePageDto commentDto) {
        return apArticleService.findList(commentDto);
    }

    /**
     * 查询文章信息
     *
     * @param id
     * @return
     */
    @Override
    public ApArticle getArticleById(Long id) {
        return apArticleMapper.selectById(id);
    }

    /**
     * 更新文章信息
     *
     * @param article
     */
    @Override
    public void updateArticle(ApArticle article) {
        apArticleMapper.updateById(article);
    }

}
